package com.example.administrator.testxingpiao.dao;

import android.content.Context;
import android.util.Log;

import com.example.administrator.testxingpiao.bean.User;
import com.example.administrator.testxingpiao.utils.ORMLiteHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/6.
 */
public class UserDao {
   /* private ORMLiteHelper orMliteHelper;
    //Dao为借口，泛型为实体类，和主键
    private static Dao<User,Integer> dao;
    private static final String TAG = "TAG";
    private static UserDao uDao;
    private UserDao(Context context){
        this.orMliteHelper=new ORMLiteHelper(context);
        try {
            dao=orMliteHelper.getDao(User.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private  static UserDao userDaoInstance(Context context){
        if (uDao==null){
            uDao=new UserDao(context);
        }
        return uDao;
    }
    public  static void add(User user,Context context){
        userDaoInstance(context);
        try {
            int state=dao.create(user);
            Log.i(TAG, "add: " + state);
        } catch (SQLException e) {
           e.printStackTrace();
        }
    }*/


    private ORMLiteHelper ormLiteHelper;
    private static Dao<User, Integer> dao;
    private static UserDao mDao;

    private UserDao(Context context){

        this.ormLiteHelper = new ORMLiteHelper(context);

        try {
            dao = ormLiteHelper.getDao(User.class);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }
    //单例Dao
    private static UserDao userDaoInstance(Context context){
        if(mDao == null){
            mDao = new UserDao(context);
        }
        return mDao;
    }

    //增
    public static int  add(User user, Context context){
        userDaoInstance(context);
        int add = -1;
        try {
            add = dao.create(user);
            log("add=" + add);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return add;
    }

    //删
    public static int  delete(Context context,User user){
        userDaoInstance(context);
        int delete = -1;
        DeleteBuilder<User, Integer> builder = dao.deleteBuilder();
        try {
            builder.where().eq("name",user.getName());
            delete = builder.delete();
            log("delete" + delete);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }
    public static int deleteAll(Context context){
        userDaoInstance(context);
        int delete = -1;
        DeleteBuilder<User, Integer> builder = dao.deleteBuilder();
        try {
            delete = builder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }

    //改
    public static void update(Context context,User user){
        userDaoInstance(context);
        UpdateBuilder<User, Integer> builder = dao.updateBuilder();
        try {
            builder.where().eq("name",user.getName());
            builder.updateColumnValue("pwd",user.getPwd());
            int update = builder.update();
            log("update = " + update);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    //查全部
    public static List<User> queryAll(Context context){
        List<User> userList= new ArrayList<>();
        userDaoInstance(context);

        try {
            userList= dao.queryForAll();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    //查一个
    public static User queryOne(Context context, User user){

        List<User> userList = new ArrayList<>();

        QueryBuilder<User, Integer> builder = dao.queryBuilder();

        try {
            builder.where().eq("name",user.getName());
            userList = builder.query();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
        return userList.get(0);
    }

    public static void log(String msg){
        Log.i("TAG",msg);
    }
}
